#include<iostream>
#include<assert.h>
using namespace std;
namespace My_list
{
	//list的节点
	template<class T>
	struct list_node
	{
		T _data;
		list_node<T>* _next;
		list_node<T>* _prev;
		list_node(const T& data=T())
			:_data(data)
			,_next(nullptr)
			,_prev(nullptr)
		{}
	};
	//list类迭代器
	template<class T>
	struct list_iterator
	{
		using Node = list_node<T>;
		using self = list_iterator<T>;
		Node* _node;

		list_iterator(Node* node)
			:_node(node)
		{}
		T& operator*()
		{
			return _node->_data;
		}
		self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		bool operator!=(const self& s) const
		{
			return _node != s._node;
		}
		bool operator==(const self& s) const
		{
			return _node == s._node;
		}
	};
	//list类
	template <class T>
	class list
	{
		using Node = list_node<T>;
	public:
		using iterator = list_iterator<T>;

		iterator begin()
		{
			return _head->_next;
		}
		iterator end()
		{
			return _head;
		}

		list()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}
		void push_back(const T& x)
		{
			insert(end(), x);
		}
		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		void insert(iterator pos, const T& x)
		{
			Node* newnode = new Node(x);
			newnode->_next = pos._node;
			newnode->_prev = pos._node->_prev;
			pos._node->_prev->_next = newnode;
			pos._node->_prev = newnode;
			++_size;
		}
		void pop_back()
		{
			erase(--end());
		}
		void pop_front()
		{
			erase(begin());
		}
		void erase(iterator pos)
		{
			assert(pos != end());
			pos._node->_prev->_next = pos._node->_next;
			pos._node->_next->_prev = pos._node->_prev;
			delete pos._node;
			--_size;
		}
		size_t size()const
		{
			return _size;
		}
		bool empty() const
		{
			return _size == 0;
		}
	private:
		Node* _head;
		size_t _size;
	};
}
void test01()
{
	My_list::list<int> it1;
	it1.push_back(1);
	it1.push_back(2);
	it1.push_back(3);
	it1.push_back(4);
	My_list::list<int>::iterator it2 = it1.begin();
	while (it2 != it1.end())
	{
		cout << *it2 << " ";
		++it2;
	}
	cout << endl;
}
void test02()
{
	My_list::list<double> it1;
	it1.push_back(1.1);
	it1.push_back(2.2);
	it1.push_back(3.3);
	it1.push_back(4.4);
	My_list::list<double>::iterator it2 = it1.begin();
	while (it2 != it1.end())
	{
		cout << *it2 << " ";
		++it2;
	}
	cout << endl;
}
int main()
{
	test01();
	test02();
	return 0;
}